package com.zenithsun.news.newsinfo.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.jdbc.Work;
import org.springframework.stereotype.Repository;

import com.syzg.common.dao.impl.BaseDAOHibernate;
import com.zenithsun.common.dao.NativeSQLDaoHibernate;
import com.zenithsun.news.newsinfo.dao.NewsdetailinfoDAO;
import com.zenithsun.news.newsinfo.dao.NewsinfoDAO;
import com.zenithsun.news.newsinfo.model.Newsdetailinfo;
import com.zenithsun.news.newsinfo.model.Newsinfo;

/**
 * Newsdetailinfo的持久化管理实现
 * @author wanglei@enersun.con.cn
 */
@Repository("newsdetailinfoDao")
public class NewsdetailinfoDAOImpl extends NativeSQLDaoHibernate<Newsdetailinfo> implements NewsdetailinfoDAO {
	
	/**
	 * listNewsReview的查询SQL<br/>
	 * 优先按is_top（是否置顶）排序，其次按publish_time(发布时间)排序
	 */
	private String SQL_listNewsdetailinfo="SELECT T.* FROM NEWSDETAILINFO T WHERE T.IS_SHOW=0 AND T.NEW_ID=? ORDER BY ORDER_NO ASC";
	
	private String SQL_saveNewsdetailinfoBatch="INSERT INTO NEWSDETAILINFO(ID,NEW_ID,PATH,REMARK,ORDER_NO,IS_SHOW) VALUES(?,?,?,?,?,?)";
	
	@Override
	protected Class<Newsdetailinfo> getEntityClass() {
		return Newsdetailinfo.class;
	}

	@Override
	public List<Newsdetailinfo> listNewsdetailinfo(String newId) throws Exception {
		SQLQuery query=getSession().createSQLQuery(SQL_listNewsdetailinfo);
		query.addEntity(getEntityClass());
		query.setParameter(0, newId);
		return query.list();
	}

	@Override
	public int saveNewsdetailinfoBatch(List<Newsdetailinfo> list) throws Exception {
		getSession().doWork(getBatchWork(list));
		return 0;
	}
	
	/**
	 * 获取执行的work
	 * @param list
	 * @return
	 * @throws Exception
	 */
	private Work getBatchWork(final List<Newsdetailinfo> list) throws Exception {
		//定义一个匿名类，实现了Work接口
			Work work=new Work(){
			public void execute(Connection connection)throws SQLException{
				//通过JDBC API执行用于批量更新的SQL语句
				PreparedStatement stmt=connection.prepareStatement(SQL_saveNewsdetailinfoBatch);
				for(Newsdetailinfo info:list){
					stmt.setString(1, info.getId());
					stmt.setString(2, info.getNewsinfo().getId());
					stmt.setString(3, info.getPath());
					stmt.setString(4, info.getRemark());
					stmt.setInt(5, info.getOrderNo());
					stmt.setShort(6, info.getIsShow());
					stmt.addBatch();
				}
				stmt.executeUpdate();
				stmt.clearBatch();
			}
		};
		return work;
	}
	/**
	 * 获取执行的work
	 * @param list
	 * @return
	 * @throws Exception
	 */
	private Work getUpdateBatchWork(final List<Newsdetailinfo> list) throws Exception {
		//定义一个匿名类，实现了Work接口
			Work work=new Work(){
			public void execute(Connection connection)throws SQLException{
				//通过JDBC API执行用于批量更新的SQL语句
				PreparedStatement stmt=connection.prepareStatement("UPDATE NEWSDETAILINFO SET ORDER_NO=? WHERE ID=?");
				for(Newsdetailinfo info:list){
					stmt.setInt(1, info.getOrderNo());
					stmt.setString(2, info.getId());
					stmt.addBatch();
				}
				stmt.executeUpdate();
				stmt.clearBatch();
			}
		};
		return work;
	}
	/**
	 * 批量更新
	 */
	@Override
	public int updateSortsNewsdetailinfoBatch(List<Newsdetailinfo> list)
			throws Exception {
		// TODO Auto-generated method stub
		
		int number = 0;
//		for (int i = 0; i < list.size(); i++) {
//			update(list.get(i));
//			number ++ ;
//		}
		getSession().doWork(getUpdateBatchWork(list));
		return number;
	}
}